//
//  YueCheMapInfoView.m
//  QMCXDriver
//
//  Created by Massimo on 2017/4/18.
//  Copyright © 2017年 Massimo. All rights reserved.
//

#import "YueCheMapInfoView.h"
#define blockHeight  13.f
#define blockWhidth  48.f
#define lineColor
#define PassngerHeight 38
#define TargetHeight 32.f
#define FinishContentHeight 72.f
#define StatusContentHeight 40.f
#import "QDashLineView.h"
#import "QPassenger.h"

@interface YueCheMapInfoView()
@property (nonatomic, assign)IBInspectable BOOL isShow;
@property (nonatomic, strong)IBInspectable UIImage *openedImage;
@property (nonatomic, strong)IBInspectable UIImage *unOpenImage;

@property (nonatomic, assign)NSInteger passngersCount;
@property (nonatomic, assign)NSInteger currentCount;
@property (nonatomic, assign)BOOL isDown;
@property (nonatomic, weak)IBOutlet UIView* infoContentView;
@property (nonatomic, weak)IBOutlet UIView* targetContentView;
@property (nonatomic, weak)IBOutlet UIView* passengersContentView;
@property (nonatomic, weak)IBOutlet UILabel* labelTargetStatins;
@property (nonatomic, weak)IBOutlet UIButton* buttonConfirm;


@property (nonatomic, weak)IBOutlet NSLayoutConstraint *passengersHeight;
@property (nonatomic, strong)NSLayoutConstraint *infoContentHeight;
@property (nonatomic, copy)SelectPassnger selectPassengerBlock;
@property (nonatomic, copy) ConfirmCallback confirmBlock;




@end


@implementation YueCheMapInfoView
 
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.



- (void)setPassngersCount:(NSInteger)count{
    _passngersCount = count;
    [self updateStatusHeader];
}
- (void)setCurrentCount:(NSInteger)count{
    _currentCount = count;
    [self updateStatusHeader];
}
- (void)sets:(BOOL)isDown{
    _isDown = isDown;
    [self updateStatusHeader];
}
- (void)setShow:(BOOL)isShow {
    if (_isShow != isShow) {
        _isShow = isShow;
    }
    
    if (isShow) {
        [self.infoContentView removeConstraint:self.infoContentHeight];
    }else{
        [self.infoContentView addConstraint:self.infoContentHeight];
    }
    [self setNeedsDisplay];
   
    
}



- (void)awakeFromNib{
    [super awakeFromNib];
    self.infoContentView.layer.masksToBounds = YES;
}

- (void)updatePassenger{
    [self.passengersContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    for (QPassenger *obj in self.passengersArr) {
        [self addPassngers:obj];
    } 
    self.passengersHeight.constant = self.passengersArr.count * PassngerHeight;
  
    [self layoutIfNeeded];
}

- (void)setPassengers:(NSArray*)passengers{
    self.passengersArr = [NSArray arrayWithArray:passengers];
    [self updatePassenger];
}



- (void)selectPassenger:(SelectPassnger)select{
    self.selectPassengerBlock = select;
}

- (void)clickConfirm:(ConfirmCallback)block{
    self.confirmBlock = block;
}
- (void)updateStatusHeader{
    NSString *string = _isDown ? @"未下车乘客":@"未上车乘客"; 
    [string stringByAppendingString:[@(_currentCount) stringValue]];
    [string stringByAppendingString:@"人(共"];
    [string stringByAppendingString:[@(_passngersCount) stringValue]];
    [string stringByAppendingString:@"人)"];
}


- (void)addPassngers:(QPassenger*)passnger{
    
    UIView *lastPassView = self.passengersContentView.subviews.lastObject;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, QScreenWidth, PassngerHeight)];
    [self.passengersContentView addSubview:view];

    __weak typeof(self) weakSelf = self;
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        lastPassView ? make.top.equalTo(lastPassView.mas_bottom): make.top.mas_equalTo(0);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(PassngerHeight);
    }];
    UIImageView *icon  =[[UIImageView alloc]initWithFrame:CGRectZero];
    UIImage *img = [UIImage imageNamed:@"icon_yueche_phone"];
    icon.image = img;
    icon.contentMode = UIViewContentModeScaleAspectFit;
    [view addSubview:icon];
    [icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(12);
        make.size.mas_equalTo(19);
    }];
    
    
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    nameLabel.font = kFont(18);
    nameLabel.textColor = LLColorRGB(51, 51, 51);
    nameLabel.text = passnger.name;
    [view addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(icon);
        make.left.equalTo(icon.mas_right).offset(5);
        make.width.mas_equalTo(60);
    }];
    
    
    UILabel *phoneLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    phoneLabel.font = kFont(16);
    phoneLabel.textColor = LLColorRGB(102, 102, 102);
    phoneLabel.text = passnger.phone;
    [view addSubview:phoneLabel];
    [phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(nameLabel);
        make.left.equalTo(nameLabel.mas_right).offset(15);
    }];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"btn_yueche_normal"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"btn_yueche_select"] forState:UIControlStateSelected];
    [view addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(nameLabel);
        make.right.mas_equalTo(-12);
        make.size.mas_equalTo(CGSizeMake(23, 23));
    }];
    
    
    [RACObserve(passnger, status) subscribeNext:^(id x) {
        button.selected = [x boolValue];
    }];
    
    [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        passnger.status = !passnger.status;
        if (weakSelf.selectPassengerBlock) {
            weakSelf.selectPassengerBlock(passnger);
        }
    }];
    
    
}


- (void)setRouteFinished{
    [self.infoContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self addFinishView];
}


- (void)addFinishView{
    
    
    
    self.isShow = YES; 
    QDashLineView *line = [[QDashLineView alloc] initWithFrame:CGRectZero];
    line.backgroundColor = [UIColor clearColor];
    [self.infoContentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(1/QScreenScale);
    }];
    
    
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectZero];
    label1.font = kFont(15);
    label1.textColor = LLColorRGB(102, 102, 102);
    label1.text = @"所有乘客已下车，该行程已完成";
    
    [self.infoContentView addSubview:label1];
    __weak typeof(self) weakSelf = self;
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.infoContentView);
        make.top.mas_equalTo(14);
    }];
    
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectZero];
    label2.font = kFont(17);
    label2.textColor = LLColorRGB(51, 51, 51);
    label2.text = @"请点击\"结束行程\"";
    
    [self.infoContentView addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label1.mas_bottom).offset(5);
        make.centerX.equalTo(weakSelf.infoContentView);
        make.bottom.mas_equalTo(-16);
    }];
    [self setNeedsDisplay];
    
}



- (void)drawRect:(CGRect)rect {
    // Drawing code
    self.layer.masksToBounds = YES;
    UIImage *image = _isShow ? self.openedImage : self.unOpenImage;
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGFloat blockX = (CGRectGetWidth(rect) - blockWhidth)/2.0;
    CGFloat blockY = CGRectGetMaxY(rect) - 12.5;
    CGFloat radius = 3;
    
    //绘制底色
    
    CGContextSaveGState(context);
    
    [[UIColor whiteColor ] setFill];
    
    
    CGContextMoveToPoint(context, 0, 0);
    CGContextAddLineToPoint(context, 0, blockY);
    CGContextAddLineToPoint(context, blockX, blockY );
    CGContextAddArcToPoint(context, blockX, blockY + blockHeight - 2 , blockX+blockWhidth/2.0, blockY+blockHeight - 2, radius);
    CGContextAddArcToPoint(context, blockX + blockWhidth, blockY + blockHeight -2, blockX+blockWhidth, blockY, radius);
    CGContextAddLineToPoint(context, blockX + blockWhidth, blockY);
    CGContextAddLineToPoint(context, CGRectGetWidth(rect), blockY);
    CGContextAddLineToPoint(context, CGRectGetWidth(rect), 0);
    CGContextClosePath(context);
    CGContextFillPath(context);
    
    CGContextRestoreGState(context);
    
    //画底部的线条
    CGContextSetRGBStrokeColor(context,211.f/255.f, 211.f/255.f, 211.f/255.f, 1.0);
    CGContextMoveToPoint(context, 0, blockY);
    CGContextAddLineToPoint(context, blockX, blockY);
    CGContextAddArcToPoint(context, blockX, blockY + blockHeight - 2 , blockX+blockWhidth/2.0, blockY+blockHeight - 2, radius);
    CGContextAddArcToPoint(context, blockX + blockWhidth, blockY + blockHeight - 2 , blockX+blockWhidth, blockY, radius);
    CGContextAddLineToPoint(context, blockX + blockWhidth, blockY);
    CGContextAddLineToPoint(context, CGRectGetWidth(rect), blockY);
    CGContextStrokePath(context);
    
    //画箭头
    CGContextSaveGState(context);
    CGFloat arrowX = (CGRectGetWidth(rect) - image.size.width)/2.0;
    CGFloat arrowY = blockY  ;
    [image drawInRect:CGRectMake(arrowX, arrowY, image.size.width, image.size.height)];
    CGContextRestoreGState(context);
    CGContextFillPath(context);
    
}



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self setShow:!self.isShow];
}

- (IBAction)actionConfirmBtn:(id)sender{
    if (self.confirmBlock) {
        self.confirmBlock(self.passengersArr);
    }
}
- (NSArray *)passengersArr{
    if (_passengersArr == nil) {
        _passengersArr = [[NSArray alloc] init];
    }
    return _passengersArr;
}

- (NSLayoutConstraint *)infoContentHeight{
    if (_infoContentHeight == nil) {
        _infoContentHeight = [NSLayoutConstraint constraintWithItem:self.infoContentView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:0];
    }
    return _infoContentHeight;
}
@end
